package com.skzh.web.controller.flightplans;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.servlet.http.HttpServletResponse;

import com.skzh.common.config.SkzhConfig;
import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.utils.SecurityUtils;
import com.skzh.common.utils.file.FileUploadUtils;
import com.skzh.common.utils.file.FileUtils;
import com.skzh.flightplans.domain.Waypoints;
import com.skzh.flightplans.utils.FlightPlansUtil;
import com.skzh.framework.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.skzh.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.enums.BusinessType;
import com.skzh.flightplans.domain.FlightPlans;
import com.skzh.flightplans.service.IFlightPlansService;
import com.skzh.common.utils.poi.ExcelUtil;
import com.skzh.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import com.skzh.flightplans.utils.TaskStatus;
import com.skzh.flightplans.utils.TaskStatusManager;

/**
 * 航线列表Controller
 *
 * @author skzh
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/flightplans/plans")
public class FlightPlansController extends BaseController {
    @Autowired
    private IFlightPlansService flightPlansService;
    @Autowired
    private ServerConfig serverConfig;
    @Autowired
    private FlightPlansUtil flightPlansUtil;
    @Autowired
    private TaskStatusManager taskStatusManager;

    /**
     * 查询航线列表列表
     */
    @ApiOperation(value = "查询航线列表列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:list')")
    @GetMapping("/list")
    public TableDataInfo list(FlightPlans flightPlans) {
        startPage();
        List<FlightPlans> list = flightPlansService.selectFlightPlansList(flightPlans);
        return getDataTable(list);
    }

    /**
     * 导出航线列表列表
     */
    @ApiOperation(value = "导出航线列表列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:export')")
    @Log(title = "航线列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FlightPlans flightPlans) {
        List<FlightPlans> list = flightPlansService.selectFlightPlansList(flightPlans);
        ExcelUtil<FlightPlans> util = new ExcelUtil<FlightPlans>(FlightPlans.class);
        util.exportExcel(response, list, "航线列表数据");
    }

    /**
     * 获取航线列表详细信息
     */
    @ApiOperation(value = "获取航线列表详细信息")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:query')")
    @GetMapping(value = "/{fpId}")
    public AjaxResult getInfo(@PathVariable("fpId") String fpId) {
        return success(flightPlansService.selectFlightPlansByFpId(fpId));
    }

    /**
     * 新增航线列表
     */
    @ApiOperation(value = "新增航线列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:add')")
    @Log(title = "航线列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FlightPlans flightPlans) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        flightPlans.setCreateBy(loginUser.getUsername());
        return toAjax(flightPlansService.insertFlightPlans(flightPlans));
    }

    /**
     * 新导入航线列表
     */
    @ApiOperation(value = "导入航线列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:add')")
    @Log(title = "航线列表", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult addUpload(@RequestBody FlightPlans flightPlans) {
        return flightPlansUtil.processFlightPlansUpload(flightPlans);
    }

    /**
     * 修改航线列表
     */
    @ApiOperation(value = "修改航线列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:edit')")
    @Log(title = "航线列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FlightPlans flightPlans) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        flightPlans.setUpdateBy(loginUser.getUsername());
        return toAjax(flightPlansService.updateFlightPlans(flightPlans));
    }

    /**
     * 删除航线列表
     */
    @ApiOperation(value = "删除航线列表")
    @PreAuthorize("@ss.hasPermi('flightplans:plans:remove')")
    @Log(title = "航线列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fpIds}")
    public AjaxResult remove(@PathVariable String[] fpIds) {
        return toAjax(flightPlansService.deleteFlightPlansByFpIds(fpIds));
    }

    /**
     * 查询任务状态
     */
    @ApiOperation(value = "查询任务状态")
    @GetMapping("/task/{taskId}")
    public AjaxResult getTaskStatus(@PathVariable String taskId) {
        TaskStatus status = taskStatusManager.getTaskStatus(taskId);
        if (status == null) {
            return AjaxResult.error("任务不存在");
        }
        return AjaxResult.success(status);
    }
}
